/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-7-11 下午2:30 Created by sky
 */
package com.jade.modules.message.sms.bestone;

import com.jade.modules.message.sms.*;
import com.jade.modules.message.sms.comm.AbstractAdapter;
import com.jade.modules.message.sms.comm.SmsRequest;
import com.jade.modules.message.sms.comm.SmsResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * Bestone协议适配器。
 *
 * @author sky
 * @version 1.0 13-7-11 下午2:30
 */
public class BestoneAdapter
        extends AbstractAdapter
{
    @Override
    protected void loadStatusMapper()
    {
        if (StringUtils.isBlank(configuration.getStatusMappperResource())) {
            configuration.setStatusMappperResource("com/jade/message/sms/bestone/bestone.properties");
        }
        super.loadStatusMapper();
    }

    public <Bind extends SmsRequest> Bind createBind()
    {
        Login login = new Login();
        login.setUsername(configuration.getUsername());
        login.setPassword(configuration.getPassword());
        return (Bind) login;
    }

    public <Ping extends SmsRequest> Ping createPing()
    {
        return null;
    }

    public SmsResponse handleNonDataRequest(SmsRequest request)
    {
        if (request instanceof Login) {
            //todo 服务器端的处理
            return null;
        }
        else if (request instanceof TimeoutNotice) {
            //返回null告知外部调用者需要关闭连接
            return null;
        }
        else {
            throw new IllegalArgumentException("Invalid non data request: " + request.getCommandId());
        }
    }

    public <S extends SmsRequest> S toSubmit(SmsMessage sms, SmsServiceParams params)
    {
        Submit submit = new Submit();
        submit.setFrom(sms.getFrom());
        submit.setTo(sms.getTo()[0]);
        submit.setServiceCode(params.getServiceCode());
        submit.setContent(sms.getContentString());
        return (S) submit;

    }

    public <D extends SmsRequest> D toDeliver(SmsMessage sms, SmsServiceParams params)
    {
        throw new UnsupportedOperationException("toDeliver unsupported");
    }

    public <R extends SmsRequest> R toReport(SmsReport report, SmsServiceParams params)
    {
        throw new UnsupportedOperationException("toDeliver unsupported");
    }

    public Object toMessage(SmsRequest request, SmsServiceParams params)
    {
        if (request instanceof Submit) {
            Submit submit = new Submit();
            SmsMessage message = new SmsMessage();
            message.setFrom(submit.getFrom());
            message.setTo(submit.getTo());
            message.setFormat(SmsConstants.FORMAT_GBK);
            message.setContent(submit.getContent());
            params.setServiceCode(submit.getServiceCode());
            return message;
        }
        else {
            throw new IllegalArgumentException("Invalid quest " + request.getCommandId());
        }
    }

    public SmsResponse createResponse(SmsRequest request, SmsResult result)
    {
        SmsResponse response = request.createResponse();
        if (request instanceof Submit) {
            SubmitResp submitResp = (SubmitResp) response;
            submitResp.setStatus(getProtocolStatus(result.getCode(0)));
            submitResp.setMessageId(result.getMsgId(0));
        }
        return response;
    }

    public SmsResult createResult(SmsRequest request, SmsResponse response)
    {
        if (response instanceof SubmitResp) {
            SubmitResp submitResp = (SubmitResp) response;
            Submit submit = (Submit) request;
            return new SmsResult(1, submitResp.getMessageId(), getStatus(response.getStatus()), null);
        }

        return new SmsResult(1, null, getStatus(response.getStatus()), null);
    }
}
